package com.book.service.impl;

import com.book.api.ReaderApi;
import com.book.pojo.Score;
import com.book.pojo.ScoreRanking;
import com.book.mapper.ScoreRankingDao;
import com.book.service.ScoreRankingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cxy
 * @since 2023-10-13 14:50:19
 */
@Service
public class ScoreRankingServiceImpl extends ServiceImpl<ScoreRankingDao, ScoreRanking> implements ScoreRankingService {

    @Autowired
    private ScoreRankingDao scoreRankingDao;
    @Autowired
    private ReaderApi readerApi;


    @Override
    public List<ScoreRanking> ScoreRanking() {
        List<Score> scores = readerApi.ScoreRanking();
        for (Score score : scores) {
            Long bookId = score.getBookId();
            scoreRankingDao.deleteByBookId(bookId);
            scoreRankingDao.add(score.getBookId(),score.getScoreAvg());
        }

        return scoreRankingDao.selectList(null);
    }

    @Override
    public void deleteByBookId(Long bookId) {
        scoreRankingDao.deleteByBookId(bookId);
    }

    @Override
    public void add(Long bookId, Double scoreAvg) {
        scoreRankingDao.add(bookId,scoreAvg);
    }
}
